using Devtodo.Models;
using Microsoft.Data.Sqlite;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.ObjectModel;
using Windows.Storage;
namespace Devtodo.Pages;

public sealed partial class Project : Page
{
  public Project()
  {
    InitializeComponent();
    LoadList();
  }
  public ObservableCollection<ProjectItem> Items { get; } = [];
  int UpdateId { get; set; } = -1;
  async void LoadList()
  {
    Items.Clear();
    var connection = new SqliteConnection("Data Source=" + ApplicationData.Current.LocalFolder.Path + "/devtodo.db");
    await connection.OpenAsync();
    var selectCmd = new SqliteCommand("SELECT * FROM project", connection);
    var reader = await selectCmd.ExecuteReaderAsync();
    while (await reader.ReadAsync())
    {
      Items.Add(new ProjectItem
      {
        Id = reader.GetInt32(0),
        Name = reader.GetString(1)
      });
    }
    await connection.CloseAsync();
  }
  void Back()
  {
    List.Visibility = Visibility.Visible;
    New.Visibility = Visibility.Collapsed;
    Update.Visibility = Visibility.Collapsed;
    UpdateId = -1;
    InsertNameInput.Text = "";
    UpdateNameInput.Text = "";
    LoadList();
  }
  void NavigateToNew(object sender, RoutedEventArgs args)
  {
    List.Visibility = Visibility.Collapsed;
    New.Visibility = Visibility.Visible;
    Update.Visibility = Visibility.Collapsed;
  }
  void NavigateToUpdate(object sender, RoutedEventArgs args)
  {
    if (sender is Button btn)
    {
      var e = (ProjectItem)btn.Tag;
      UpdateId = e.Id;
      UpdateNameInput.Text = e.Name;
    }
    List.Visibility = Visibility.Collapsed;
    New.Visibility = Visibility.Collapsed;
    Update.Visibility = Visibility.Visible;
  }
  void NavigateBack(object sender, RoutedEventArgs args)
  {
    Back();
  }
  async void NewProject(object sender, RoutedEventArgs args)
  {
    try
    {
      if (InsertNameInput.Text.Length == 0)
      {
        ContentDialog dialog = new()
        {
          XamlRoot = XamlRoot,
          Title = "提示",
          Content = "请输入名称",
          PrimaryButtonText = "确定",
          DefaultButton = ContentDialogButton.Primary
        };
        await dialog.ShowAsync();
        return;
      }
      var connection = new SqliteConnection("Data Source=" + ApplicationData.Current.LocalFolder.Path + "/devtodo.db");
      await connection.OpenAsync();
      var incertCmd = new SqliteCommand("INSERT INTO project (name) VALUES ('" + InsertNameInput.Text + "')", connection);
      await incertCmd.ExecuteNonQueryAsync();
      await connection.CloseAsync();
      ContentDialog successDialog = new()
      {
        XamlRoot = XamlRoot,
        Title = "提示",
        Content = "新增成功",
        PrimaryButtonText = "确定",
        DefaultButton = ContentDialogButton.Primary
      };
      await successDialog.ShowAsync();
      Back();
    }
    catch (Exception err)
    {
      ContentDialog dialog = new()
      {
        XamlRoot = XamlRoot,
        Title = "错误",
        Content = err.Message,
        PrimaryButtonText = "确定",
        DefaultButton = ContentDialogButton.Primary
      };
      await dialog.ShowAsync();
    }
  }
  async void UpdateProject(object sender, RoutedEventArgs args)
  {
    if (UpdateNameInput.Text.Length == 0)
    {
      ContentDialog dialog = new()
      {
        XamlRoot = XamlRoot,
        Title = "提示",
        Content = "请输入名称",
        PrimaryButtonText = "确定",
        DefaultButton = ContentDialogButton.Primary
      };
      await dialog.ShowAsync();
      return;
    }
    var connection = new SqliteConnection("Data Source=" + ApplicationData.Current.LocalFolder.Path + "/devtodo.db");
    await connection.OpenAsync();
    var updateCmd = new SqliteCommand("UPDATE project SET name='" + UpdateNameInput.Text + "' WHERE id=" + UpdateId, connection);
    await updateCmd.ExecuteNonQueryAsync();
    await connection.CloseAsync();
    ContentDialog successDialog = new()
    {
      XamlRoot = XamlRoot,
      Title = "提示",
      Content = "修改成功",
      PrimaryButtonText = "确定",
      DefaultButton = ContentDialogButton.Primary
    };
    await successDialog.ShowAsync();
    Back();
  }
}